<?php

namespace app\admin\service;

use app\admin\model\QuestionsAnswers;
use app\admin\model\questionsCategory;
use app\admin\model\QuestionsList;
use app\common\validate\CommonValidate;
use think\exception\ValidateException;
use think\facade\Request;

class QuestionsService
{
    public static function goEditCategory()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'title|名称' => 'require',
                'status|状态' => 'require',
            ];
            $message = [
                'title.require' => "分类名称不能为空",
                'status.require' => "分类状态不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            try {
                $data['title'] = str_replace(' ', '', trim($data['title']));
                if ($data['id']) {
                    $res = QuestionsCategory::update($data);
                } else {
                    $exist = QuestionsCategory::where('title', $data['title'])->find();
                    if ($exist) return ['msg' => '记录存在', 'code' => 201];
                    $res = QuestionsCategory::create($data);
                }
                if ($res) return ['msg' => '操作成功', 'code' => 200];
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goCategoryStatus()
    {
        $data = Request::post();
        $model = QuestionsCategory::find($data['id']);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save([
                'status' => $data['status'],
            ]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goCategoryRemove()
    {
        $data = Request::post();
        $info = QuestionsCategory::find($data['id']);
        if ($info->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $info->delete();
//            return ['msg' => '删除功能只做演示', 'code' => 200];
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goEditQuestion()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'cid|分类' => 'require',
                'title|名称' => 'require',
                'type|类型' => 'require',
                'difficulty|难度' => 'require|min:1|max:5',
                'status|状态' => 'require',
            ];
            $message = [
                'cid.require' => "请选择问题分类",
                'title.require' => "问题不能为空",
                'type.require' => "请选择问题类型",
                'status.require' => "问题状态不能为空",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            try {
                $data['title'] = str_replace(' ', '', trim($data['title']));
                if ($data['id']) {
                    $res = QuestionsList::update($data);
                } else {
                    $exist = QuestionsList::where(['cid' => $data['cid'], 'title' => $data['title']])->find();
                    if ($exist) return ['msg' => '记录存在', 'code' => 201];
                    $res = QuestionsList::create($data);
                    if ($data['type'] == 3 && $res['id']) QuestionsAnswers::createJudgeOption($res['id']);
                }
                if ($res) return ['msg' => '操作成功', 'code' => 200];
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goQuestionStatus()
    {
        $data = Request::post();
        $model = QuestionsList::find($data['id']);
        if ($model->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $model->save([
                'status' => $data['status'],
            ]);
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goQuestionRemove()
    {
        $data = Request::post();
        $info = QuestionsList::find($data['id']);
        if ($info->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $info->delete();
//            return ['msg' => '删除功能只做演示', 'code' => 200];
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }

    public static function goEditAnswer()
    {
        if (Request::isPost()) {
            $data = Request::post();
            //验证
            $rule = [
                'qid|问题' => 'require',
                'title|答案' => 'require',
                'correct|状态' => 'require',
            ];
            $message = [
                'qid.require' => "参数错误",
                'title.require' => "答案不能为空",
                'correct.require' => "请选择答案是否正确",
            ];
            $check = Request::checkToken('__token__', $data);
            if (false === $check) {
                throw new ValidateException('invalid token');
            }
            if (($msg = CommonValidate::check($rule, $message, $data)) !== true)
                return ['msg' => $msg, 'code' => 201];
            try {
                $data['title'] = str_replace(' ', '', trim($data['title']));
                if ($data['id']) {
                    $res = QuestionsAnswers::update($data);
                } else {
                    $exist = QuestionsAnswers::where(['qid' => $data['qid'], 'title' => $data['title']])->find();
                    if ($exist) return ['msg' => '记录存在', 'code' => 201];
                    $res = QuestionsAnswers::create($data);
                }
                if ($res) return ['msg' => '操作成功', 'code' => 200];
            } catch (\Exception $e) {
                return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
            }
        }
    }

    public static function goAnswerRemove()
    {
        $data = Request::post();
        $info = QuestionsAnswers::find($data['id']);
        if ($info->isEmpty()) return ['msg' => '数据不存在', 'code' => 201];
        try {
            $info->delete();
//            return ['msg' => '删除功能只做演示', 'code' => 200];
        } catch (\Exception $e) {
            return ['msg' => '操作失败' . $e->getMessage(), 'code' => 201];
        }
    }
}